package com.example.hotel_lxy.sql;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

import androidx.annotation.Nullable;

public class UserSQLiteOpenHelper extends SQLiteOpenHelper {


    private static final String TAG = "UserSQLiteOpenHelper";
    public static final String sql = "create table SqliteDemo (id integer primary key autoincrement," +
            " name text(20),address text(20),ids text(20),avatar text(100),mobile text(30),token text(10000000))";
    public static final String sql1 = "create table test1 (id integer primary key autoincrement," +
            " name text(20),address text(20),ids text(20),avatar text(100),mobile text(30),token text(10000000))";
    public UserSQLiteOpenHelper(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);//创建数据库调用方法
    }

    /**
     * 数据库创建时调用
     *
     * 只有在app第一次安装时才会调用这个方法
     * @param db
     */
    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(sql);
        Log.d(TAG,"onCreate");
    }

    /**
     * 数据库升级时调用
     *
     * 只有在上面super执行的时候才会执行这个方法
     *
     * @param db
     * @param oldVersion
     * @param newVersion
     */
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

        switch (oldVersion){
            case 1:
                db.execSQL(sql1);
                break;
        }
    }
}
